using System.Collections.Generic;

namespace NFreeMarker.Cache
{
    /**
     * Strong cache storage is a cache storage that simply wraps a {@link Map}.
     * It holds a strong reference to all objects it was passed, therefore prevents
     * the cache from being purged during garbage collection.
     * This class is thread-safe to the extent that its underlying map is. The 
     * default implementation uses a concurrent map on Java 5 and above, so it is
     * thread-safe in that case.
     * @author Attila Szegedi
     * @version $Id: StrongCacheStorage.java,v 1.3 2003/09/22 20:47:03 ddekany Exp $
     *
     */

    public class StrongCacheStorage : IConcurrentCacheStorage
    {
        private readonly IDictionary<object, object> _map = ConcurrentMapFactory.CreateMap();

        /**
         * Returns true if the underlying Map is a {@code ConcurrentMap}.
         */

        public bool IsConcurrent()
        {
            return ConcurrentMapFactory.IsConcurrent(_map);
        }

        public object Get(object key)
        {
            return _map[key];
        }

        public void Put(object key, object value)
        {
            _map.Add(key, value);
        }

        public void Remove(object key)
        {
            _map.Remove(key);
        }

        public void Clear()
        {
            _map.Clear();
        }
    }
}